<template>
  <div class="file-item">
    <div class="file-item__img" :class="{ 'file-item--selected': props.data?.selected }">
      <yk-image :src="url" width="200" height="200" :is-lazy="true" fit="contain"></yk-image>
      <yk-space class="file-item__img--tool" size="s">
        <IconDeleteOutline class="files__tool-delete" @click="deleteFile"></IconDeleteOutline>
        <yk-popconfirm title="选择分组" trigger="click" placement="top-end" :teleported="true" :append-to-body="true"
          :offset="[0, 12]" popper-class="subset-popover" @cancel="cancel" @confirm="confirm">
          <IconSwitchOutline class="files__tool-switch"></IconSwitchOutline>
          <template #content>
            <yk-scrollbar ref="scrollbar" height="148px" class="subset">
              <div @click="changeOption(item.id)" v-for="item in subsetStore.data" :key="item.id" class="subset__item"
                :class="{ 'subset-selected': subsetSelectedId === item.id }">
                {{ item.name }} {{ item.value }}
              </div>
            </yk-scrollbar>
          </template>
        </yk-popconfirm>
      </yk-space>
      <div class="file-item__img--check">
        <IconTickMinOutline style="color: #fff; font-size: 24px" @click="checkFile" />
      </div>
    </div>
    <div class="file-item__name">
      <p class="file-item__name-file">{{ props.data?.file_name }}</p>
      <p>.{{ props.data?.format }}</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, getCurrentInstance } from 'vue'
import type { FileData } from '@/utils/interface'
import { useSubsetStore } from '@/stores/subset';
import { baseImgPath } from '@/utils/env';
import './file.less'
const subsetStore = useSubsetStore();

type FileItemProps = {
  data?: FileData
}
const props = withDefaults(defineProps<FileItemProps>(), {})

const emits = defineEmits(['changeSubsetId', 'delete', 'selected'])

// 图片地址
const url = computed(() => {
  const imgRul = baseImgPath + '/' + props.data?.url
  return imgRul
})
// 分类选择
const subsetSelectedId = ref<number | string>(props.data?.subset_id ?? -1)

// 切换分组
const changeOption = (e: number | string) => {
  subsetSelectedId.value = e
}

const proxy: any = getCurrentInstance()?.proxy
function cancel() {
  proxy.$message({ type: 'warning', message: '已取消' })
}
function confirm() {
  // 如果当前选择和之前不同时
  const data = {
    id: props.data?.id,
    subsetId: subsetSelectedId.value
  }
  emits('changeSubsetId', data)
  // proxy.$message({ type: 'primary', message: '你点击了确认按钮' })
}
// 删除
const deleteFile = () => {
  emits('delete', props.data?.id, props.data?.url)
}

// 选择
const checkFile = () => {

  emits('selected', props.data?.id)
}
</script>

<style lang="less" scoped>
// 颜色变量
@primary-color: #3a7bd5;
@primary-light: rgba(58, 123, 213, 0.1);
@primary-hover: rgba(58, 123, 213, 0.2);
@text-color: #2c3e50;
@text-light: #5e6c84;
@bg-color: #ffffff;
@bg-light: #f8fafd;
@border-color: #e8ecf1;
@shadow-color: rgba(0, 0, 0, 0.08);
@transition: all 0.3s cubic-bezier(0.25, 1, 0.5, 1);

// 文件卡片样式
.file-item {
  position: relative;
  transition: @transition;



  // 图片容器
  &__img {
    position: relative;
    border-radius: 12px;
    overflow: hidden;
    background: @bg-light;
    box-shadow: 0 4px 12px @shadow-color;
    transition: @transition;
    aspect-ratio: 1 / 1;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid transparent;

    // 图片悬停效果
    &:hover {
      box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
      transform: translateY(-4px);
      background: @primary-light;

      .file-item__img--check {
        opacity: 1;
        transform: translateY(0);
      }

      .file-item__img--tool {
        opacity: 1;
        transform: translateY(0);
      }
    }

    // 工具栏
    &--tool {
      position: absolute;
      right: 10px;
      bottom: 10px;
      opacity: 0;
      transform: translateY(10px);
      transition: @transition;
      display: flex;
      gap: 6px;
      z-index: 5;

      // 图标按钮样式
      .files__tool-delete,
      .files__tool-switch {
        width: 34px;
        height: 34px;
        padding: 7px;
        border-radius: 50%;
        background: rgba(255, 255, 255, 0.9);
        backdrop-filter: blur(4px);
        color: @text-light;
        cursor: pointer;
        transition: @transition;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

        &:hover {
          transform: scale(1.1);
        }
      }

      // 删除按钮特殊效果
      .files__tool-delete:hover {
        color: #e74c3c;
        background: rgba(255, 255, 255, 0.95);
      }

      // 切换按钮特殊效果
      .files__tool-switch:hover {
        color: @primary-color;
        background: rgba(255, 255, 255, 0.95);
      }
    }

    // 选择框
    &--check {
      width: 34px;
      height: 34px;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.9);
      backdrop-filter: blur(4px);
      position: absolute;
      left: 10px;
      top: 10px;
      cursor: pointer;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      opacity: 0;
      transform: translateY(-10px);
      transition: @transition;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;

      // 添加点击涟漪效果
      &::before {
        content: '';
        position: absolute;
        width: 100%;
        height: 100%;
        background: radial-gradient(circle, rgba(58, 123, 213, 0.6) 0%, rgba(58, 123, 213, 0) 70%);
        transform: scale(0);
        opacity: 0;
        transition: transform 0.5s, opacity 0.5s;
      }

      &:active::before {
        transform: scale(3);
        opacity: 1;
        transition: transform 0.3s, opacity 0.3s;
      }

      .yk-icon {
        opacity: 0;
        transition: all 0.3s cubic-bezier(0.25, 1, 0.5, 1);
        font-size: 18px;
        color: @text-light;
        z-index: 1;
      }

      &:hover {
        background: rgba(255, 255, 255, 0.95);
        transform: scale(1.1);
        box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);

        .yk-icon {
          color: @primary-color;
          opacity: 0.9;
          transform: scale(1.2);
        }
      }
    }
  }

  // 选中状态
  &--selected {
    .file-item__img {
      border: 2px solid @primary-color !important;
      background: @primary-light !important;
      box-shadow: 0 6px 16px rgba(58, 123, 213, 0.15) !important;

      // 选中状态下的选择框
      .file-item__img--check {
        opacity: 1 !important;
        transform: translateY(0) !important;
        background: @primary-color !important;
        box-shadow: 0 3px 12px rgba(58, 123, 213, 0.3) !important;
        animation: pulse 1s ease-out; // 添加脉冲动画

        // 图标样式增强
        .yk-icon {
          opacity: 1 !important;
          color: white !important;
          transform: scale(1.3);
          filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.2));
          animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275); // 添加弹出动画
        }
      }

      // 选中状态下隐藏工具栏
      .file-item__img--tool {
        opacity: 0 !important;
        pointer-events: none;
      }

      &:hover .file-item__img--tool {
        opacity: 1 !important;
        pointer-events: all;
      }
    }
  }

  // 文件名区域
  &__name {
    padding: 12px 6px 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 2px;

    p {
      margin: 0;
      font-size: 14px;
      color: @text-color;
    }

    // 文件名部分
    &-file {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      max-width: 80%;
      font-weight: 500;
    }
  }
}

// 分组选择下拉菜单
.subset {
  width: 180px;
  z-index: 1001; // 增加z-index确保不被遮挡

  &__item {
    padding: 8px 12px;
    cursor: pointer;
    border-radius: 6px;
    transition: @transition;
    margin-bottom: 4px;
    font-size: 14px;

    &:hover {
      background-color: @primary-light;
      color: @primary-color;
    }
  }

  &-selected {
    background: linear-gradient(135deg, @primary-color, lighten(@primary-color, 15%));
    color: white;
    font-weight: 500;

    &:hover {
      background: linear-gradient(135deg, @primary-color, lighten(@primary-color, 15%)) !important;
      color: white !important;
    }
  }
}

// 增强全局弹窗样式 - 使用:global而不是:deep，确保样式能够作用到body级别
:global(.subset-popover) {
  z-index: 10001 !important; // 提高弹窗层级，高于其他所有元素
  border-radius: 12px !important;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2) !important;
  border: 1px solid rgba(232, 236, 241, 0.8) !important;
  overflow: hidden !important;
  transform-origin: center bottom !important;
  animation: popperFadeIn 0.2s ease-out !important;
}

:global(.yk-popconfirm__action) {
  justify-content: flex-end !important;
  padding: 8px 12px !important;
  border-top: 1px solid @border-color !important;
  background-color: #fafbfc !important;
}

:global(.yk-popover__title) {
  font-weight: 600 !important;
  padding: 12px 16px !important;
  border-bottom: 1px solid @border-color !important;
  color: @text-color !important;
}

@keyframes popperFadeIn {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.95);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

// 增强弹窗样式
:deep(.yk-popconfirm) {
  z-index: 9999 !important; // 提高弹窗层级
}

:deep(.yk-popover__content) {
  border-radius: 12px !important;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15) !important;
  border: 1px solid rgba(232, 236, 241, 0.8) !important;
  overflow: hidden !important;
}

// 响应式调整
@media (max-width: 1200px) {
  .file-item {
    &__img {
      border-radius: 10px;

      &--tool,
      &--check {
        transition: opacity 0.2s ease;
      }
    }
  }
}

@media (max-width: 992px) {
  .file-item {
    &__name {
      padding-top: 10px;

      p {
        font-size: 13px;
      }
    }
  }
}

@media (max-width: 768px) {
  .file-item {
    &__img {
      border-radius: 8px;

      // 在平板和手机上默认显示控制按钮，便于触控操作
      &--tool {
        opacity: 1;
        transform: translateY(0);
      }

      &--check {
        opacity: 0.7;
        transform: translateY(0);
      }

      // 工具按钮适配触控设备
      &--tool {

        .files__tool-delete,
        .files__tool-switch {
          width: 36px;
          height: 36px;
          padding: 8px;
        }
      }
    }
  }

  // 移动端弹窗优化
  :deep(.yk-popover__content) {
    max-width: 85vw !important; // 限制在移动设备上的最大宽度
  }

  .subset {
    width: 100%;
    max-height: 200px;

    &__item {
      padding: 10px 12px; // 增大触摸区域
      font-size: 14px;
    }
  }
}

@media (max-width: 576px) {
  .file-item {
    &__img {
      box-shadow: 0 3px 10px @shadow-color;

      &:hover {
        transform: translateY(-2px);
      }

      &--tool {
        right: 6px;
        bottom: 6px;
        gap: 4px;
      }

      &--check {
        left: 6px;
        top: 6px;
        width: 30px;
        height: 30px;
      }
    }

    &__name {
      padding-top: 8px;

      p {
        font-size: 12px;
      }
    }
  }
}

@media (max-width: 480px) {
  .file-item {
    &__img {

      // 在超小屏幕上，简化悬停动画
      &:hover {
        transform: none;
        box-shadow: 0 3px 10px @shadow-color;
      }
    }
  }
}

// 添加动画
@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(58, 123, 213, 0.6);
  }

  70% {
    box-shadow: 0 0 0 8px rgba(58, 123, 213, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(58, 123, 213, 0);
  }
}

@keyframes popIn {
  0% {
    transform: scale(0.8);
    opacity: 0.5;
  }

  50% {
    transform: scale(1.4);
  }

  100% {
    transform: scale(1.3);
    opacity: 1;
  }
}
</style>
